package com.aistar.service.employer.impl;

import com.aistar.mapper.*;
import com.aistar.model.dto.employer.ResumeAndDeliveryDTO;
import com.aistar.model.entity.*;
import com.aistar.service.employer.IEmployerResumeService;
import com.aistar.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: IEmployerResumeService
 * @author: LiuWenXiu
 * @date: 2021/9/27 9:58
 * @Description: TODO...本类用来演示:
 */
@Service
@Transactional
public class EmployerResumeServiceImpl implements IEmployerResumeService {

    @Autowired
    private ResumeMapper resumeMapper;
    @Autowired
    private EmployerMapper employerMapper;
    @Autowired
    private RecruitmentMapper recruitmentMapper;
    @Autowired
    private DeliveryMapper deliveryMapper;
    @Autowired
    private EmployeeBlacklistMapper employeeBlacklistMapper;
    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private ResumeViewMapper resumeViewMapper;

    @Autowired
    private IEmployerResumeService employerResumeService;


    @Override
    public List<Resume> loadResumeCenter(Integer employerId) {
        // 加载所有的简历
        // 这里需要有一个判断->要根据id进行判断,如果该简历的屏蔽列表里有这个公司,那就不展示->好难啊TAT
        // 首先根据
        Integer companyId = employerMapper.findCompanyIdById(employerId);
        List<EmployeeBlacklist> employeeBlacklists = employeeBlacklistMapper.findEmployeeIdByCompanyId(companyId);
        List<Integer> employeeIds = new ArrayList<>();
        List<Integer> resumeIds = new ArrayList<>();
        // 判断该黑名单里是否有值,如果有则进行下一步
        // 如果黑名单根本没值,则直接返回所有的
        if (null == employeeBlacklists || employeeBlacklists.size() == 0)
            return resumeMapper.findAll();
        // 否则遍历黑名单,获取黑名单列表屏蔽本公司的求职者id
        for (EmployeeBlacklist employeeBlacklist : employeeBlacklists) {
            employeeIds.add(employeeBlacklist.getEmployeeId());
        }
        // 此时employeeIds已经获取了所有屏蔽本公司的求职者id,再根据求职者id获取所有的简历id
        // 遍历employeeIds
        for (Integer employeeId : employeeIds) {
            // 根据求职者id获取所有的简历id
            List<Resume> temp = resumeMapper.findByEmployeeId(employeeId);
            if (null == temp || temp.size() == 0)
                continue;
            for (Resume resume : temp) {
                resumeIds.add(resume.getId());
            }
        }
        // 如果此时简历集合为空,则也是直接返回所有的 - 但是这种情况一般是不可能的
        if (null == resumeIds || resumeIds.size() == 0)
            return resumeMapper.findAll();
        List<Resume> resumeListAll = resumeMapper.findAll();
        List<Resume> resumeList = new ArrayList<>();
        // 遍历所有的简历
        for (Resume resume : resumeListAll) {
            if (resumeIds.contains(resume.getId()))
                continue;
            resumeList.add(resume);
        }
        return resumeList;
    }

    @Override
    public List<Resume> loadResumeCenterPage(Integer employerId, Integer page, Integer limit) {
        // 所有的简历
        List<Resume> resumeListAll = employerResumeService.loadResumeCenter(employerId);
        if (null == resumeListAll || resumeListAll.size() == 0)
            return null;
        // 上限
        Integer upper = (page - 1) * limit;
        // 下限
        Integer lower = page * limit;
        // 临时参数
        Integer temp = 0;
        List<Resume> resumeList = new ArrayList<>();
        // 遍历所有的简历
        for (Resume resume : resumeListAll) {
            if (temp >= upper && temp < lower) {
                resumeList.add(resume);
            }
            temp++;
        }
        return resumeList;

    }

    @Override
    public List<ResumeAndDeliveryDTO> loadResumeByPage(Integer id, Integer page, Integer limit, String status) {
        List<ResumeAndDeliveryDTO> resumeAndDeliveryDTOS = new ArrayList<>();
        // 上限
        Integer upper = (page - 1) * limit;
        // 下限
        Integer lower = page * limit;
        // 临时参数
        Integer temp = 0;
        // 首先获取该招聘者所在公司id
        Integer companyId = employerMapper.findCompanyIdById(id);
        // 获取该公司的所有职位信息
        List<Recruitment> recruitmentList = recruitmentMapper.findByCompanyId(companyId);
        // 判断
        if (null == recruitmentList || recruitmentList.size() == 0)
            return null;
        Integer count = 1;
        // 遍历集合
        for (Recruitment recruitment : recruitmentList) {
            // 根据职位查找出所有的简历投递信息
            List<Delivery> deliveryList = deliveryMapper.findByRecruitmentAndStatus(recruitment.getId(), status);
            // 判断是否有简历投递
            if (null != deliveryList && deliveryList.size() > 0) {
                // 遍历简历投递列表 根据简历投递情况 获取简历信息
                for (Delivery delivery : deliveryList) {
                    if (temp >= upper && temp < lower) {
                        ResumeAndDeliveryDTO resumeAndDeliveryDTO = new ResumeAndDeliveryDTO();
                        Resume resume = resumeMapper.findById(delivery.getResumeId());
                        resumeAndDeliveryDTO.setNum(count++);
                        resumeAndDeliveryDTO.setResume(resume);
                        resumeAndDeliveryDTO.setRecruitment(recruitment);
                        resumeAndDeliveryDTO.setDelivery(delivery);
                        resumeAndDeliveryDTOS.add(resumeAndDeliveryDTO);
                    }
                    temp++;
                }
            }
        }
        return resumeAndDeliveryDTOS;
    }

    @Override
    public List<ResumeAndDeliveryDTO> loadResumeByPageAndTime(Integer id, Integer page, Integer limit, String status, Integer getDay) {
        // 首先加载所有的按时间的简历记录
        List<ResumeAndDeliveryDTO> resumeAndDeliveryDTOList = loadResumeAllByTime(id,status,getDay);
        if(null == resumeAndDeliveryDTOList || resumeAndDeliveryDTOList.size() == 0)
            return null;
        // 遍历集合
        List<ResumeAndDeliveryDTO> resumeAndDeliveryDTOS = new ArrayList<>();
        // 上限
        Integer upper = (page - 1) * limit;
        // 下限
        Integer lower = page * limit;
        // 临时参数
        Integer temp = 0;
        for (ResumeAndDeliveryDTO resumeAndDeliveryDTO : resumeAndDeliveryDTOList) {
            if(temp>=upper && temp<lower){
                resumeAndDeliveryDTOS.add(resumeAndDeliveryDTO);
            }
            temp++;
        }
        return resumeAndDeliveryDTOS;
    }

    @Override
    public List<ResumeAndDeliveryDTO> loadResumeAll(Integer id, String status) {
        List<ResumeAndDeliveryDTO> resumeAndDeliveryDTOS = new ArrayList<>();
        // 首先获取该招聘者所在公司id
        Integer companyId = employerMapper.findCompanyIdById(id);
        // 获取该公司的所有职位信息
        List<Recruitment> recruitmentList = recruitmentMapper.findByCompanyId(companyId);
        // 判断
        if (null == recruitmentList || recruitmentList.size() == 0)
            return null;
        Integer count = 1;
        // 遍历集合
        for (Recruitment recruitment : recruitmentList) {
            // 根据职位查找出所有的简历投递信息
            List<Delivery> deliveryList = deliveryMapper.findByRecruitmentAndStatus(recruitment.getId(), status);
            // 判断是否有简历投递
            if (null != deliveryList && deliveryList.size() > 0) {
                // 遍历简历投递列表 根据简历投递情况 获取简历信息
                for (Delivery delivery : deliveryList) {
                    ResumeAndDeliveryDTO resumeAndDeliveryDTO = new ResumeAndDeliveryDTO();
                    Resume resume = resumeMapper.findById(delivery.getResumeId());
                    resumeAndDeliveryDTO.setNum(count++);
                    resumeAndDeliveryDTO.setResume(resume);
                    resumeAndDeliveryDTO.setRecruitment(recruitment);
                    resumeAndDeliveryDTO.setDelivery(delivery);
                    resumeAndDeliveryDTOS.add(resumeAndDeliveryDTO);
                }
            }
        }
        return resumeAndDeliveryDTOS;
    }

    @Override
    public List<ResumeAndDeliveryDTO> loadResumeAllByTime(Integer id, String status, Integer getDay) {
        // 获取所有的简历
        List<ResumeAndDeliveryDTO> resumeAllList = loadResumeAll(id, status);
        List<ResumeAndDeliveryDTO> newResumeAllList = loadByTime(resumeAllList,getDay);
        // 进行排序
        newResumeAllList.sort((o1,o2)->{
            if(o1.getDelivery().getDeliveryTime().getTime()>o2.getDelivery().getDeliveryTime().getTime())
                return -1;
            else if(o1.getDelivery().getDeliveryTime().getTime()<o2.getDelivery().getDeliveryTime().getTime())
                return 1;
            return 0;
        });
        return newResumeAllList;
    }

    @Override
    public List<ResumeAndDeliveryDTO> loadByTime(List<ResumeAndDeliveryDTO> resumeAndDeliveryDTOList, Integer getDay) {
        if(null==resumeAndDeliveryDTOList || resumeAndDeliveryDTOList.size()==0)
            return null;
        List<ResumeAndDeliveryDTO> newResumeAllList = new ArrayList<>();
        // 对职位的时间进行筛选
        // 获取今天的毫秒数
        long today = new Date().getTime();
        // 定义一个变量表示需要界限的毫秒数
        long dayTime = 0L;
        if (getDay == 3) {
            // 全部的简历
            return resumeAndDeliveryDTOList;
        } else if (getDay == 0) {
            // 今天的简历
            // nowDay的getTime - 简历的投递时间的getTime必须小于一天=>24*60*60*60*1000
            dayTime = 24L * 60 * 60 *  1000;
        } else if (getDay == 1) {
            // 三天内的简历
            // nowDay的getTime - 简历的投递时间的getTime必须小于三天=>3*24*60*60*60*1000
            dayTime = 3 * 24L * 60 * 60 * 1000;
        } else if (getDay == 2) {
            // 一周内的简历
            // nowDay的getTime - 简历的投递时间的getTime必须小于七天=>7*24*60*60*60*1000
            dayTime = 7 * 24L * 60 * 60  * 1000;
        }
        // 对时间进行筛选 - 遍历旧的集合
        for (ResumeAndDeliveryDTO resumeAndDeliveryDTO : resumeAndDeliveryDTOList) {
            // 获取简历投递的时间戳[如果是2021-11-02 16:03:59 算是2021-11-02的时间]
            String delDate = DateUtil.format(resumeAndDeliveryDTO.getDelivery().getDeliveryTime(),"yyyy-MM-dd");
            Date deliveryDate = DateUtil.parse(delDate,"yyyy-MM-dd");
            if(today - deliveryDate.getTime() < dayTime){
                newResumeAllList.add(resumeAndDeliveryDTO);
            }
        }
        return  newResumeAllList;
    }

    @Override
    public List<ResumeAndDeliveryDTO> loadResumeByRecruitmentIdPage(Integer id, Integer page, Integer limit, String status) {
        List<ResumeAndDeliveryDTO> resumeAndDeliveryDTOList = new ArrayList<>();
        // 根据职位查找出所有的简历投递信息
        List<Delivery> deliveryList = deliveryMapper.findByRecruitmentAndStatus(id, status);
        // 如果没有简历就直接返回null
        if (null == deliveryList || deliveryList.size() == 0) {
            return null;
        }
        // 上限
        Integer upper = (page - 1) * limit;
        // 下限
        Integer lower = page * limit;
        // 临时参数
        Integer temp = 0;
        // 表示有简历投递记录
        Integer count = 1;
        // 遍历简历投递列表 根据简历投递情况 获取简历信息
        for (Delivery delivery : deliveryList) {
            if (temp >= upper && temp < lower) {
                ResumeAndDeliveryDTO resumeAndDeliveryDTO = new ResumeAndDeliveryDTO();
                Resume resume = resumeMapper.findById(delivery.getResumeId());
                resumeAndDeliveryDTO.setNum(count++);
                resumeAndDeliveryDTO.setResume(resume);
                resumeAndDeliveryDTO.setRecruitment(recruitmentMapper.findById(id));
                resumeAndDeliveryDTO.setDelivery(delivery);
                resumeAndDeliveryDTOList.add(resumeAndDeliveryDTO);
            }
            temp++;
        }
        return resumeAndDeliveryDTOList;
    }

    @Override
    public List<ResumeAndDeliveryDTO> loadResumeByRecruitmentIdPageAndTime(Integer id, Integer page, Integer limit, String status, Integer getDay) {
        List<ResumeAndDeliveryDTO> resumeAndDeliveryDTOList = loadResumeByRecruitmentIdAllAndTime(id,status,getDay);
        if(null == resumeAndDeliveryDTOList || resumeAndDeliveryDTOList.size() == 0)
            return null;
        // 遍历集合
        List<ResumeAndDeliveryDTO> resumeAndDeliveryDTOS = new ArrayList<>();
        // 上限
        Integer upper = (page - 1) * limit;
        // 下限
        Integer lower = page * limit;
        // 临时参数
        Integer temp = 0;
        for (ResumeAndDeliveryDTO resumeAndDeliveryDTO : resumeAndDeliveryDTOList) {
            if(temp >= upper && temp<lower){
                resumeAndDeliveryDTOS.add(resumeAndDeliveryDTO);
            }
            temp++;
        }
        return resumeAndDeliveryDTOS;
    }

    @Override
    public List<ResumeAndDeliveryDTO> loadResumeByRecruitmentIdAll(Integer id, String status) {
        List<ResumeAndDeliveryDTO> resumeAndDeliveryDTOList = new ArrayList<>();
        // 根据职位查找出所有的简历投递信息
        List<Delivery> deliveryList = deliveryMapper.findByRecruitmentAndStatus(id, status);
        // 如果没有简历就直接返回null
        if (null == deliveryList || deliveryList.size() == 0) {
            return null;
        }
        // 表示有简历投递记录
        Integer count = 1;
        // 遍历简历投递列表 根据简历投递情况 获取简历信息
        for (Delivery delivery : deliveryList) {
            ResumeAndDeliveryDTO resumeAndDeliveryDTO = new ResumeAndDeliveryDTO();
            Resume resume = resumeMapper.findById(delivery.getResumeId());
            resumeAndDeliveryDTO.setNum(count++);
            resumeAndDeliveryDTO.setResume(resume);
            resumeAndDeliveryDTO.setRecruitment(recruitmentMapper.findById(id));
            resumeAndDeliveryDTO.setDelivery(delivery);
            resumeAndDeliveryDTOList.add(resumeAndDeliveryDTO);
        }
        resumeAndDeliveryDTOList.sort((o1,o2)->{
            if(o1.getDelivery().getDeliveryTime().getTime()>o2.getDelivery().getDeliveryTime().getTime())
                return -1;
            else if(o1.getDelivery().getDeliveryTime().getTime()<o2.getDelivery().getDeliveryTime().getTime())
                return 1;
            return 0;
        });
        return resumeAndDeliveryDTOList;
    }

    @Override
    public List<ResumeAndDeliveryDTO> loadResumeByRecruitmentIdAllAndTime(Integer id, String status, Integer getDay) {
        List<ResumeAndDeliveryDTO> resumeAndDeliveryDTOList = loadResumeByRecruitmentIdAll(id,status);
        List<ResumeAndDeliveryDTO> resumeAndDeliveryDTOS = loadByTime(resumeAndDeliveryDTOList,getDay);

        return resumeAndDeliveryDTOS;
    }

    @Override
    public int updateResumeStatus(Integer id, String status) {
        return deliveryMapper.updateStatusById(id, status);
    }

    @Override
    public int delResumeDelivery(Integer id) {
        return deliveryMapper.delDeliveryById(id);
    }

    @Override
    public int delRecruitmentBatchById(String ids) {
        String[] id = ids.split(",");
        Integer[] idArr = new Integer[id.length];
        // 遍历数组
        for (int i = 0; i < id.length; i++) {
            idArr[i] = Integer.valueOf(id[i]);
        }
        int count = 0;
        for (Integer i : idArr) {
            count += deliveryMapper.delDeliveryById(i);
        }
        return count;
    }


//    @Override
//    public int createInterview(Integer deliveryId, Date interviewTime, String interviewAddress) {
//        // 定义变量
//        Integer count = 0;
//        // 首先需要创建邀约面试表的数据  - 创建面试邀约
//        // 根据邀约面试表id获取简历id和职位id
//        Delivery delivery = deliveryMapper.findById(deliveryId);
//        Interview interview = new Interview();
//        interview.setResumeId(delivery.getResumeId());
//        interview.setRecruitmentId(delivery.getRecruitmentId());
//        interview.setInterviewAddress(interviewAddress);
//        interview.setInterviewTime(interviewTime);
//        count += interviewMapper.createInterview(interview);
//
//        // 发送邀约申请 - 发送email
//        // 获取简历对应的邮箱
//        String email = resumeMapper.findById(delivery.getResumeId()).getEmail();
//        if (null == email)
//            return 0;
//        // 获取邀约面试的公司名称
//        Integer companyId = recruitmentMapper.findById(delivery.getRecruitmentId()).getCompanyId();
//        String companyName = companyMapper.findById(companyId).getName();
//
//        // 发送邮件的过程
//        SimpleMailMessage msg = new SimpleMailMessage();
//        // 发送方
//        msg.setFrom("2294014200@qq.com");
//        // 设置标题
//        msg.setSubject("轻聘-面试邀约");
//        // 内容是验证码
//        // 您好,xx公司邀请您于xx时间xx地点参加面试,详情内容可以登录轻聘查看
//        String message = "您好," + companyName + "公司邀请您于" + DateUtil.format(interviewTime, "yyyy-MM-dd HH:mm:ss") + "在" + interviewAddress + "参加面试.\n";
//        message += "更多详细内容请您登录轻聘官网进行查看,谢谢!";
//        //接收方
//        msg.setText(message);
//        msg.setTo(email);
//
//        // 真正的发送
//        mailSender.send(msg);
//        // 修改简历投递表的状态- 将该行改变为已邀约
//        count += deliveryMapper.updateStatusById(deliveryId, "已邀约");
//        return count;
//    }

    @Override
    public int resumeView(Integer resumeId, Integer employerId, Date viewTime) {
        // 首先要根据简历id和招聘者的公司id查询简历查看表看是否存在数据,如果有则更新查看时间并令查看次数++,如果没有则需要创建简历查看信息.

        // 根据招聘者id获取招聘者的公司id和公司名称
        Integer companyId = employerMapper.findCompanyIdById(employerId);
        // 根据简历id和招聘者id查看是否能查询出信息
        ResumeView resumeView = resumeViewMapper.findByResumeIdAndCompanyId(resumeId, companyId);
        int count = 0;
        if (null != resumeView) {
            // 说明这个公司已经看过我的简历了 只需要更新查看时间以及查看次数
            resumeView.setCount(resumeView.getCount() + 1);
            resumeView.setViewTime(new Date());
            count = resumeViewMapper.updateResumeView(resumeView);
        } else {
            // 否则表示这个公司是第一次看我的简历,需要创建记录
            // 获取公司名称
            String companyName = companyMapper.findById(companyId).getName();
            // 创建简历查看实体
            ResumeView resumeViewNew = new ResumeView();
            resumeViewNew.setEmployerId(employerId);
            resumeViewNew.setCompanyId(companyId);
            resumeViewNew.setCompanyName(companyName);
            resumeViewNew.setViewTime(viewTime);
            resumeViewNew.setResumeId(resumeId);
            // 首先创建简历查看表的数据
            count = resumeViewMapper.insertResumeView(resumeViewNew);
        }

        // 简历表的某个字段也要增加
        count += resumeMapper.updateFrequency(resumeId);
        return count;
    }


}
